package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.MyGiftBagAdapter
import com.lykj.llw.box.data.bean.response.MyGiftBagListResponse
import com.lykj.llw.box.databinding.ActMyGiftBagBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 我的礼包
 * ================================================
 *
 * @author gosha
 * @date 2023/10/17-14:22
 */
class MyGiftBagActivity : BaseActivity() {
    lateinit var binding: ActMyGiftBagBinding

    private val mAdapter = MyGiftBagAdapter()
    private val mData = ArrayList<MyGiftBagListResponse.DataDTO>()
    private var pageSize = 10
    private var totalPage = 1
    private var pageNo = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActMyGiftBagBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "我的礼包"
        binding.titleBar.tvRightOption.visibility = View.INVISIBLE

        initRecyclerView()
    }

    private fun initRecyclerView() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvMyGift.layoutManager = linearLayoutManager
        binding.rcvMyGift.setHasFixedSize(true)
        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_0dp, resources.newTheme()
            )
        )
        binding.rcvMyGift.addItemDecoration(dividerItemDecoration)
        mAdapter.addChildClickViewIds(R.id.tv_copy)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_copy) {
                ClipboardUtils.copyText(mData[position].card)
                ToastUtils.showShort(this@MyGiftBagActivity, "复制到剪切板成功")
            }
        }
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无礼包数据"
        mAdapter.setEmptyView(emptyView)
        mAdapter.data = mData
        binding.rcvMyGift.adapter = mAdapter
    }

    //获取我的礼包
    private fun obtainMyGiftBag() {
        clearBodyParam()
        initBodyParam()
        bodyParams["pageNo"] = pageNo
        bodyParams["pageSize"] = pageSize
        RetrofitManager.instance.obtainMyGiftBag(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@MyGiftBagActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                //解析gson
                var bean = GsonUtils.fromJson(
                    responseBody, MyGiftBagListResponse::class.java
                )

                if (bean.data.isNotEmpty()) {
                    mData.addAll(bean.data)
                    mAdapter.notifyDataSetChanged()
                }
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@MyGiftBagActivity, msg)
                }
            }
        }, bodyParams)
    }

    override fun initData() {
        obtainMyGiftBag()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.srfGift.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mData.clear()
                mAdapter.notifyDataSetChanged()
                pageNo = 1
                obtainMyGiftBag()
                refreshLayout.finishRefresh(350, true, false)
                refreshLayout.resetNoMoreData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (pageNo == totalPage) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                    ToastUtils.showShort(this@MyGiftBagActivity, "没有更多礼包数据了")
                    return
                }
                pageNo++
                obtainMyGiftBag()
                refreshLayout.finishLoadMore(350)
            }
        })
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}